package com.frejoys.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.*;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.GoodsListVo;
import com.frejoys.common.vo.admin.GoodsVo;
import com.frejoys.dao.entity.Goods;
import com.frejoys.service.CategoryService;
import com.frejoys.service.GoodsService;
import com.frejoys.service.StoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Tag(name = "商品管理", description = "商品管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminGoodsController {


    private final GoodsService goodsService;


    private final CategoryService categoryService;


    private final StoreService storeService;

    private final RedissonClient redissonClient;


    @Operation(summary = "商品列表")
    @PostMapping("/goods/list")
    public Result<IPage<GoodsListVo>> storeList(@RequestBody @Validated StoreIdAndNameDto dto) {
        return Result.success(goodsService.listInfo(dto));
    }

    @Operation(summary = "商品详情")
    @PostMapping("/goods/detail")
    public Result<GoodsVo> detail(@RequestBody @Validated IdDto dto) {
        Goods goods = goodsService.getById(dto.getId());
        GoodsVo goodsVo = BeanUtil.copyProperties(goods, GoodsVo.class);
        if (goodsVo != null) {
            Map<Integer, String> storeIdNameMap = storeService.getIdNameMap(Collections.singletonList(goodsVo.getStoreId()));
            goodsVo.setStoreName(storeIdNameMap.get(goodsVo.getStoreId()));
            Map<Integer, String> categoryIdNameMap = categoryService.getIdNameMap(Collections.singletonList(goodsVo.getCategoryId()));
            goodsVo.setCategoryName(categoryIdNameMap.get(goodsVo.getCategoryId()));
        }
        return Result.success(goodsVo);
    }

    @Operation(summary = "添加商品")
    @PostMapping("/goods/addInfo")
    public Result addInfo(@RequestBody @Validated GoodsAddDto dto) {
        goodsService.addInfo(dto);
        return Result.success();
    }

    @Operation(summary = "编辑商品")
    @PostMapping("/goods/upInfo")
    public Result upInfo(@RequestBody @Validated GoodsUpDto dto) {
        goodsService.upInfo(dto);
        return Result.success();
    }


    @Operation(summary = "删除商品")
    @PostMapping("/goods/delInfo")
    public Result delInfo(@RequestBody @Validated IdDto dto) {
        goodsService.delInfo(dto.getId());
        return Result.success();
    }


//    @PostMapping("/goods/sale")
//    @Operation(summary = "后台审核商品上架")
//    public Result goodsOnSale(@RequestBody @Validated IdDto dto){
//        goodsService.goodsOnSale(dto);
//        return Result.success();
//    }

    @Operation(summary = "编辑商品上下架状态")
    @PostMapping("/goods/upGoodsShelves")
    public Result upGoodsShelves(@RequestBody @Validated UpShelvesDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.upStoreGoodsLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                goodsService.upGoodsShelves(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }

}
